{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8f9910fa",
   "metadata": {},
   "source": [
    "* 日期： 2022-10-19 \\ week09(周三）\n",
    "* 课程：python"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0dff8220",
   "metadata": {},
   "source": [
    "### 购物车程序项目\n",
    "要求：1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "　　   2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "　　   3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "\n",
    "　　   4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64e737c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "##购物车项目\n",
    "product_list = [ \n",
    "    \"自行车 299\",\n",
    "    \"Iphone 14 Pro 9999\",\n",
    "    \"NFT盲盒 28\",\n",
    "    \"许老师的python书 80\"\n",
    "]\n",
    "print(product_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "dceefa4f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "product_list\n",
      "请输入余额：30\n",
      "30\n",
      "请输入商品编号：2\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for -: 'int' and 'list'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_24740/3134427727.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     14\u001b[0m \u001b[0muser_choice\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"请输入商品编号：\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     15\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m \u001b[1;32mif\u001b[0m \u001b[0mAlipay\u001b[0m \u001b[1;33m>=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mAlipay\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mproduct_list\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     17\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"enough\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     18\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'int' and 'list'"
     ]
    }
   ],
   "source": [
    "product_list = [\n",
    "    [\"自行车\", 299],\n",
    "    [\"Iphone 14 Pro\", 9999],\n",
    "     [\"NFT盲盒\", 28],\n",
    "     [\"许老师的python书\", 80]\n",
    "]\n",
    "print(\"product_list\")\n",
    "\n",
    "shopping_car = []\n",
    "\n",
    "Alipay = int(input(\"请输入余额：\"))\n",
    "print(Alipay)\n",
    "\n",
    "user_choice = input(\"请输入商品编号：\")\n",
    "\n",
    "if Alipay >= int(Alipay) - product_list:\n",
    "    print(\"enough\")\n",
    "else:\n",
    "    print(\"钱不够\")\n",
    "\n",
    "print(shopping_car)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ee7b0e23",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "Interrupted by user",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_24740/1180360732.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      6\u001b[0m ]\n\u001b[0;32m      7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0mAlipay\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"请输入余额：\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      9\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[1;34m(self, prompt)\u001b[0m\n\u001b[0;32m   1004\u001b[0m                 \u001b[1;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1005\u001b[0m             )\n\u001b[1;32m-> 1006\u001b[1;33m         return self._input_request(\n\u001b[0m\u001b[0;32m   1007\u001b[0m             \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprompt\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1008\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"shell\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel\\kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[1;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[0;32m   1049\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1050\u001b[0m                 \u001b[1;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1051\u001b[1;33m                 \u001b[1;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Interrupted by user\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1052\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1053\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Invalid Message:\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: Interrupted by user"
     ]
    }
   ],
   "source": [
    "product_list = [\n",
    "    [\"自行车\", 299],\n",
    "    [\"Iphone 14 Pro\", 9999],\n",
    "     [\"NFT盲盒\", 28],\n",
    "     [\"许老师的python书\", 80]\n",
    "]\n",
    "\n",
    "Alipay = int(input(\"请输入余额：\"))\n",
    "print()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "5ac5e81a",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (Temp/ipykernel_24740/846716124.py, line 24)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"C:\\Users\\1\\AppData\\Local\\Temp/ipykernel_24740/846716124.py\"\u001b[1;36m, line \u001b[1;32m24\u001b[0m\n\u001b[1;33m    else:\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "## \n",
    "product_list = [\n",
    "     [\"自行车\", 299],\n",
    "    [\"Iphone 14 Pro\", 9999],\n",
    "     [\"NFT盲盒\", 28],\n",
    "     [\"许老师的python书\", 80]\n",
    "]\n",
    "\n",
    "shopping_car = []\n",
    "\n",
    "Alipay = input(\"请输入余额：\")\n",
    "print(\"---商品列表----\")\n",
    "for i in range(4):\n",
    "    print(product_list[i])\n",
    "    \n",
    "user_choice = input(\"请输入商品编号：\")\n",
    "#情景1：用户输入了商品编号：shopping\n",
    "if user_choice.isdigit():\n",
    "    ##判断钱够不够，商品是谁？==> product_list[user_choice][1]\n",
    "    if Alipay >= product_list[int(user_choice)][1]: #买完商品剩余的钱\n",
    "        Alipay = Alipay - product_list[int(user_choice)][1]\n",
    "        shopping_car.append(product_list[int(user_choice)])\n",
    "    print(\"您购买的商品已增加到购物车~~\",Alipay)\n",
    "    else:\n",
    "        print(\"您的余额不足，请更换支付方式~~\")\n",
    "#情景2：用户输入\"q\"，退出购买\n",
    "    elif user_chioce == \"q\":\n",
    "        print(\"您的购物车：\",shopping_car)\n",
    "        print(\"您的余额还有：\",Alipay)\n",
    "        break\n",
    "else:\n",
    "        print(\"您输入的商品不存在或者输入有误，请重新尝试\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "476f0b0b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
